<template>
  <div>
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
        <!-- <el-form-item prop="basicdataCheckFacility">
                <el-input size="small" v-model="searchForm.basicdataCheckFacility" placeholder="设备" clearable></el-input>
         </el-form-item> -->
         <el-form-item prop="basicdataCheckPart" label="部件名称">
                <!-- <el-input  v-model="searchForm.basicdataCheckPart" placeholder="部位" clearable></el-input> -->
                <el-select v-model="searchForm.basicdataCheckPart" placeholder="请选择部件名称" @change="handleChangeUnits">
                 <el-option v-for="item in partsData" :key="item.id" :label="'[' + item.code + ']' + item.name" :value="item.id" ref="units"></el-option>
                </el-select>
         </el-form-item>
         <el-form-item prop="basicdataCheckComponent" label="零件名称">
                <el-input  v-model="searchForm.basicdataCheckComponent" placeholder="零件名称" clearable  ></el-input>
                <!-- el-select v-model="component" placeholder="请选择零件" @change="handleChangePart" style="width:100%;" v-loading = 'isTwoDropDownBoxLoading' >
                  <el-option v-for="item in searchForm.componentData" :key="item.fileCode" :label= "'[' + item.fileCode + ']' +item.fileDevicename" :value="item.fileCode" ></el-option>
                </el-select> -->
         </el-form-item>
         <el-form-item prop="createDate" label="创建时间">
                <el-input  v-model="searchForm.createDate" placeholder="创建时间" clearable  ></el-input>
         </el-form-item>
        <!-- el-form-item prop="basicdataCheckProject">
                <el-input size="small" v-model="searchForm.basicdataCheckProject" placeholder="项目" clearable></el-input>
         </el-form-item>
         <el-form-item prop="basicdataCheckContent">
                <el-input size="small" v-model="searchForm.basicdataCheckContent" placeholder="内容" clearable></el-input>
         </el-form-item>
         <el-form-item prop="basicdataCheckStarus">
                  <el-radio-group v-model="searchForm.basicdataCheckStarus">
                    <el-radio v-for="item in $dictUtils.getDictList('device_state')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                  </el-radio-group>
         </el-form-item> -->
         <!-- <el-form-item prop="basicdataCheckJob">
                  <el-checkbox-group
                    v-model="searchForm.basicdataCheckJob">
                    <el-checkbox v-for="basicdataCheckJob in $dictUtils.getDictList('check_job')" :label="basicdataCheckJob.label" :key="basicdataCheckJob.id">{{basicdataCheckJob.value}}</el-checkbox>
                  </el-checkbox-group>
         </el-form-item>
         <el-form-item prop="basicdataCheckTime">
                <el-date-picker
                  v-model="searchForm.basicdataCheckTime"
                  type="datetime"
                  size="small"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间">
                </el-date-picker>
         </el-form-item>
         <el-form-item prop="basicdataCheckCheckPeople.id">
            <user-select :limit='1' size="small" :value="searchForm.basicdataCheckCheckPeople.id" @getValue='(value) => {searchForm.basicdataCheckCheckPeople.id=value}'></user-select>
         </el-form-item>
         <el-form-item prop="basicdataCheckFixPeople.id">
            <user-select :limit='1' size="small" :value="searchForm.basicdataCheckFixPeople.id" @getValue='(value) => {searchForm.basicdataCheckFixPeople.id=value}'></user-select>
         </el-form-item>
         <el-form-item prop="basicdataCheckPostPeople.id">
            <user-select :limit='1' size="small" :value="searchForm.basicdataCheckPostPeople.id" @getValue='(value) => {searchForm.basicdataCheckPostPeople.id=value}'></user-select>
         </el-form-item>
         <el-form-item prop="basicdataCheckMethod">
                  <el-checkbox-group
                    v-model="searchForm.basicdataCheckMethod">
                    <el-checkbox v-for="basicdataCheckMethod in $dictUtils.getDictList('check_method')" :label="basicdataCheckMethod.label" :key="basicdataCheckMethod.id">{{basicdataCheckMethod.value}}</el-checkbox>
                  </el-checkbox-group>
         </el-form-item>
         <el-form-item prop="remarks">
                <el-input size="small" v-model="searchForm.remarks" placeholder="备注信息" clearable></el-input>
         </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="refreshList()" >查询</el-button>
            <el-button @click="resetSearch()" >重置</el-button>
          </el-form-item>
      </el-form>
      <el-calendar v-model="value">
         <template
            slot="dateCell"
            slot-scope="{date, data}">
            <p :class="data.isSelected ? 'is-selected' : ''">
              {{ data.day.split('-').slice(1).join('-') }} {{ data.isSelected ? '✔️' : ''}}
            </p>
          </template>
      </el-calendar>
        <!-- 导入导出-->
      <el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
         <el-form-item>
          <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
         </el-form-item>
         <el-form-item prop="loginName">
            <el-upload
              class="upload-demo"
              :action="`${this.$http.BASE_URL}/basicdata/standard/basicdataCheckStandardMain/import`"
              :on-success="uploadSuccess"
               :show-file-list="true">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
        </el-form-item>
      </el-form>
      <el-row>
        <!-- <el-button v-if="hasPermission('basicdata:standard:basicdataCheckStandardMain:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('basicdata:standard:basicdataCheckStandardMain:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button> -->
        <el-button-group class="pull-right">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      size="medium"
      @expand-change="detail"
      class="table">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column type="expand">
      <template slot-scope="scope">
      <el-tabs>
            <el-tab-pane label="设备点检附表">
                  <el-table
                  border
                  size="medium"
                  :data="scope.row.basicdataCheckFormList"
                  style="width: 100%">
                <el-table-column
                    prop="checkCategory"
                    label="类别">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("basicdata_category", scope.row.checkCategory, '-') }}
                    </template>
                  </el-table-column>
                   <el-table-column
                    prop="name"
                    label="指标名称">
                  </el-table-column>
                <el-table-column
                    prop="checkUnit"
                    label="计量单位">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("basicdata_symbol_measure", scope.row.checkUnit, '-') }}
                    </template>
                  </el-table-column>
                <el-table-column
                    prop="checkJudgeSymbol"
                    label="判断符号">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("basicdata_symbol", scope.row.checkJudgeSymbol, '-') }}
                    </template>
                  </el-table-column>
                <el-table-column
                    prop="checkUp"
                    show-overflow-tooltip
                    label="上限">
                  </el-table-column>
                <el-table-column
                    prop="checkLimit"
                    show-overflow-tooltip
                    label="极限">
                  </el-table-column>
                <el-table-column
                    prop="checkJudge"
                    label="是否手动">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("yes_no", scope.row.checkJudge, '-') }}
                    </template>
                  </el-table-column>
                <el-table-column
                    prop="checkContent"
                    show-overflow-tooltip
                    label="点检标准">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
      </template>
      </el-table-column>
    <!-- <el-table-column
        prop="basicdataCheckNumber"
        show-overflow-tooltip
        sortable="custom"
        label="序号"
        v-if="false">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('basicdata:standard:basicdataCheckStandardMain:edit')" @click="edit(scope.row.id)">{{scope.row.basicdataCheckNumber}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('basicdata:standard:basicdataCheckStandardMain:view')"  @click="view(scope.row.id)">{{scope.row.basicdataCheckNumber}}</el-link>
              <span v-else>{{scope.row.basicdataCheckNumber}}</span>
            </template>
      </el-table-column> -->
    <el-table-column
        prop="basicdataCheckFacility"
        show-overflow-tooltip
        sortable="custom"
        label="设备名称"
        fixed="left"
        width="110px">
      </el-table-column>
    <el-table-column
        prop="basicdataCheckPart"
        show-overflow-tooltip
        sortable="custom"
        label="部件名称"
        width="160px">
        <template slot-scope="scope">
         <span >{{ '[' + scope.row.partDevice.fileCode + ']' + scope.row.partDevice.fileDevicename}}</span>
        </template>
      </el-table-column>
    <el-table-column
        prop="unitName"
        show-overflow-tooltip
        sortable="custom"
        label="零件名称"
        width="110px">
      </el-table-column>
    <el-table-column
        prop="basicdataCheckProject"
        show-overflow-tooltip
        sortable="custom"
        label="项目">
      </el-table-column>
    <el-table-column
        prop="basicdataCheckContent"
        show-overflow-tooltip
        sortable="custom"
        label="内容">
      </el-table-column>
    <el-table-column
        prop="basicdataCheckStarus"
        show-overflow-tooltip
        sortable="custom"
        label="设备状态"
        width="110px">
        <template slot-scope="scope">
             {{ $dictUtils.getDictLabel("device_state", scope.row.basicdataCheckStarus, '-') }}
        </template>
      </el-table-column>
  <el-table-column
        prop="basicdataCheckJobreturn"
        show-overflow-tooltip
        sortable="custom"
        label="点检分工"
        width="110px">
        <template slot-scope="scope" >
              {{scope.row.basicdataCheckJob.map( (item)=> { return $dictUtils.getDictLabel("check_job", item, '-')}).join(",") }}
              <!-- {{scope.row.basicdataCheckJobreturn.split(",").map( (item)=> { return $dictUtils.getDictLabel("check_job", item, '-')}).join(",")  }} -->
        </template>
      </el-table-column>
    <el-table-column
        prop="basicdataCheckTime"
        show-overflow-tooltip
        sortable="custom"
        label="点检时间"
        width="110px">
      </el-table-column>
    <!-- <el-table-column
        prop="basicdataCheckCheck"
        show-overflow-tooltip
        sortable="custom"
        label="点检">
      </el-table-column> -->
      <el-table-column
          prop="basicdataCheckCheckUnit"
          show-overflow-tooltip
          sortable="custom"
          label="点检周期">
          <template slot-scope="scope">
              <span>{{scope.row.basicdataCheckCheck + scope.row.basicdataCheckCheckUnit}} </span>
          </template>
        </el-table-column>
      <el-table-column
        prop="basicdataCheckCheckPeople.name"
        show-overflow-tooltip
        sortable="custom"
        label="点检责任人"
        width="120px">
      </el-table-column>
      <el-table-column
          prop="basicdataCheckFixTime"
          show-overflow-tooltip
          sortable="custom"
          label="检修时间"
          width="110px">
        </el-table-column>
    <!-- <el-table-column
        prop="basicdataCheckFix"
        show-overflow-tooltip
        sortable="custom"
        label="检修">
       <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("basticdata_check_period", scope.row.basicdataCheckFix, '-') }}
        </template>
      </el-table-column> -->
      <el-table-column
          prop="basicdataCheckFixUnit"
          show-overflow-tooltip
          sortable="custom"
          label="检修周期">
          <template slot-scope="scope">
              <span>{{scope.row.basicdataCheckFix + scope.row.basicdataCheckFixUnit}} </span>
          </template>
        </el-table-column>
      <el-table-column
        prop="basicdataCheckFixPeople.name"
        show-overflow-tooltip
        sortable="custom"
        label="检修责任人"
        width="120px">
      </el-table-column>
      <el-table-column
          prop="basicdataCheckPostTime"
          show-overflow-tooltip
          sortable="custom"
          label="岗位时间"
          width="110px">
        </el-table-column>
    <el-table-column
        prop="basicdataCheckPost"
        show-overflow-tooltip
        sortable="custom"
        label="岗位周期">
       <template slot-scope="scope">
            <span>{{ scope.row.basicdataCheckPost + scope.row.basicdataCheckPostUnit}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column
          prop="basicdataCheckPostUnit"
          show-overflow-tooltip
          sortable="custom"
          label="周期">
        </el-table-column> -->
      <el-table-column
        prop="basicdataCheckPostPeople.name"
        show-overflow-tooltip
        sortable="custom"
        label="岗位责任人"
        width="120px">
      </el-table-column>
      <el-table-column
        prop="createDate"
        show-overflow-tooltip
        sortable="custom"
        label="创建时间"
        width="120px">
      </el-table-column>
   <el-table-column
        prop="basicdataCheckMethodreturn"
        show-overflow-tooltip
        sortable="custom"
        label="点检方法"
        width="110px">
        <template slot-scope="scope">
              {{scope.row.basicdataCheckMethod.map( (item)=> { return $dictUtils.getDictLabel("check_method", item, '-')}).join(",") }}
               <!-- {{scope.row.basicdataCheckMethodreturn.split(",").map( (item)=> { return $dictUtils.getDictLabel("check_method", item, '-')}).join(",")  }} -->
        </template>
      </el-table-column>
    <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息"
        width="110px">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
    <BasicdataCheckStandardMainForm  ref="basicdataCheckStandardMainForm" @refreshDataList="refreshList"></BasicdataCheckStandardMainForm>
  </div>
</template>

<script>
  import BasicdataCheckStandardMainForm from '../../basicdata/standard/BasicdataCheckStandardMainForm.vue'
  import UserSelect from '@/components/userSelect'
  export default {
    props: {
      /**
       * 设备对象，包含fileCode、fileDevicename等
       */
      partDevice: {
        id: '', // 当前树节点的设备id
        code: '',
        name: '', // 设备名称
        level: '',  // 设备层级
        parents: [] // 设备的所有祖先，第一个为根节点，如果当前节点为根节点，则parents.length===0
      }
    },
    data () {
      return {
        selectData: '',
        searchForm: {
          basicdataCheckFacility: '',
          basicdataCheckPart: '',
          basicdataCheckComponent: '',
          basicdataCheckProject: '',
          basicdataCheckContent: '',
          basicdataCheckStarus: '',
          basicdataCheckJob: [],
          basicdataCheckTime: '',
          basicdataCheckPostTime: '',
          unitName: '',
          basicdataCheckFixTime: '',
          partDevice: {
            id: '', // 当前树节点的设备id
            code: '',
            name: '', // 设备名称
            level: '',  // 设备层级
            parents: [] // 设备的所有祖先，第一个为根节点，如果当前节点为根节点，则parents.length===0
          },
          basicdataCheckCheckPeople: {
            id: ''
          },
          basicdataCheckFixPeople: {
            id: ''
          },
          basicdataCheckPostPeople: {
            id: ''
          },
          basicdataCheckMethod: [],
          remarks: '',
          createDate: ''
        },
        dataList: [],
        partsData: [], // 部件数据
        componentData: [], // 零件数据
        parts: '', // 首级下拉框prop
        component: '', // 子级下拉框prop
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: true,
        isImportCollapse: false,
        loading: false,
        value: new Date()
      }
    },
    components: {
      UserSelect,
      BasicdataCheckStandardMainForm
    },
    activated () {
      this.refreshList()
    },
    watch: {
      value (newVal, oldVal) {
        console.log(newVal)
      },
      'partDevice.id': {
         handler (newPartDeviceId, oldPartDeviceId) {
          this.searchForm.partDevice.id = newPartDeviceId
          this.getNextNode(newPartDeviceId)
        },
        immediate: true // 第一次绑定数据时就触发
      },
      // 监测首级下拉框变化
      parts (newVal, oldVal) {
        this.dropDownBoxAssociated(newVal)
      }
    },
    methods: {
      // 选中下拉框所返回的函数
      handleChangeUnits (val) {
      // 传进来的val是select组件选中的value值，也就是一个fruitEnName
        var obj = {}
        obj = this.partsData.find(function (item) {
          return item.fileCode === val
        })
        this.searchForm.basicdataCheckPart = obj.fileDevicename
      },
      // 获取下一层级节点
      // 需要传入想要获取下一级树的数据的父级id参数（比如传三级id此方法获取4级数据）
      getNextNode (presentNode) {
        this.$http({
          url: `/basicdata/basictree/partDeviceNode/nextLevelTreeData`,
          method: 'get',
          params: {
            id: presentNode
          }
        }).then(({ data }) => {
          this.partsData = data.data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        console.log(this.searchForm)
        // this.searchForm.createDate = '2019-'
        this.$http({
          url: '/basicdata/standard/basicdataCheckStandardMain/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
              for (let var1 in this.dataList) {
                this.dataList[var1].basicdataCheckPostPeople["name"] = ''
                if (this.dataList[var1].basicdataCheckPostPeople.id.indexOf(',') !== -1) {
                  this.dataList[var1].basicdataCheckPostPeople.id.split(',').forEach((id) => {
                    this.$http.get(`/sys/user/queryById?id=${id}`).then(({data}) => {
                      if (data.user && data.user.id !== '') {
                          this.dataList[var1].basicdataCheckPostPeople["name"] += data.user.name + ','
                      }
                    })
                  })
                }
              }
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 二级下拉框与一级下拉框关联方法(传的参数是一级下拉框的id值)
      dropDownBoxAssociated (newVal) {
        this.isTwoDropDownBoxLoading = true
        this.$http({
          url: `/basicdata/basictree/partDeviceNode/nextLevelTreeData`,
          method: 'get',
          params: {
            id: newVal
          }
        }).then(({ data }) => {
          this.componentData = data.data
           this.isTwoDropDownBoxLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.basicdataCheckStandardMainForm.init('add', '', this.partDevice)
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.basicdataCheckStandardMainForm.init('edit', id, this.partDevice)
      },
      // 查看
      view (id) {
        this.$refs.basicdataCheckStandardMainForm.init('view', id, this.partDevice)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/basicdata/standard/basicdataCheckStandardMain/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
     // 查看详情
      detail (row) {
        this.$http.get(`/basicdata/standard/basicdataCheckStandardMain/queryById?id=${row.id}`).then(({data}) => {
          this.dataList.forEach((item, index) => {
            if (item.id === row.id) {
              item.basicdataCheckFormList = data.basicdataCheckStandardMain.basicdataCheckFormList
            }
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/basicdata/standard/basicdataCheckStandardMain/import/template')
      },
      exportExcel () {
        this.$utils.download('/basicdata/standard/basicdataCheckStandardMain/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.searchForm.basicdataCheckComponent = ''
        this.searchForm.basicdataCheckPart = ''
        this.refreshList()
      }
    }
  }
</script>
